import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
//import 'package:fluttertoast/fluttertoast.dart';
import 'package:ivy_ezviz_plugin/control/EzvizUIKitControl.dart';
import 'package:ivy_ezviz_plugin/ivy_ezviz_plugin.dart';

class Page01 extends StatefulWidget {
  @override
  _Page01State createState() => _Page01State();
}

class _Page01State extends State<Page01> {
  String _platformVersion1 = 'Unknown';
  EzvizUIKitControl ezvizUIKitControl;
  EzvizUIKitControl ezvizUIKitControl1;
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }
  @override
  void dispose() {
    ezvizUIKitControl?.dispose();
    super.dispose();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion1;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion1 = await IvyEzvizPlugin.platformVersion;
    } on PlatformException {
      platformVersion1 = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion1 = platformVersion1;
    });
  }


  double widthD;
  double heightD;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: [
            Container(
              child: Text('Running on: $_platformVersion1\n'),
            ),

            Container(
              width: double.infinity,
              height: 200,
              child: LayoutBuilder(builder: (_, constraints) {
                return EzvizUIKitViewWidget(
                  ezvizUIKitViewCreatedCallback:(controller){
                    ezvizUIKitControl = controller;
                  },
                  AppKey: '0e70040f865440f499adc1dd9f220172',
                  accessToken:
                  'at.cwrgoc4iacgn1vhlaq6o7sqk3i2crto8-13k9o4rpvp-1w4vk6z-k66u3pfrl',
                  playUrl: 'ezopen://open.ys7.com/624905335/1.live',
                  callback: (event){
                    /*Fluttertoast.showToast(
                      //backgroundColor: COLOR_THEME,
                        msg: event.toString(),
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.CENTER,
                        timeInSecForIos: 2,
                        fontSize: 16.0);*/
                  },
                );
              }),
            ),
            /*Container(
              width: double.infinity,
              height: 200,
              child: LayoutBuilder(builder: (_, constraints) {
                return EzvizUIKitViewWidget(
                  ezvizUIKitViewCreatedCallback:(controller){
                    ezvizUIKitControl1 = controller;
                  },
                  AppKey: 'xxxxx',
                  accessToken:
                  'xxxxx',
                  playUrl: 'xxxxx',
                  callback: (event){
                    Fluttertoast.showToast(
                      //backgroundColor: COLOR_THEME,
                        msg: event.toString(),
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.CENTER,
                        timeInSecForIos: 2,
                        fontSize: 16.0);
                  },
                );
              }),
            ),*/
            Wrap(
              alignment: WrapAlignment.center,
              spacing: 10,
              children: [
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.startPlay();
                    },
                    child: Text('播放')),

                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.stopPlay();
                    },
                    child: Text('停止')),

                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.releasePlayer();
                    },
                    child: Text('释放资源')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl1
                          .setUrl("XXXXXX");
                    },
                    child: Text('更换播放地址2')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl
                          .setUrl("ezopen://open.ys7.com/D21837011/1.live");
                    },
                    child: Text('更换播放地址')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl
                          .setUrl("XXXXXX");
                    },
                    child: Text('更换播放地址')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.pausePlay();
                    },
                    child: Text('暂停播放，回放专用接口')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.resumePlay();
                    },
                    child: Text('恢复播放，回放专用接口')),
                ElevatedButton(
                    onPressed: () async {
                      /**
                       * 获取当前状态：
                       * EZUIPlayer.STATUS_INIT = 0;
                       * EZUIPlayer.STATUS_START = 1;
                       * EZUIPlayer.STATUS_STOP = 2;
                       * EZUIPlayer.STATUS_PLAY = 3;
                       * EZUIPlayer.STATUS_PAUSE = 4;
                       * @return
                       */
                      Map map = await ezvizUIKitControl.getStatus();
                      print(map);
                    },
                    child: Text('获取当前状态')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.seekPlayback('2020-10-10');
                    },
                    child: Text('seek播放位置,回放使用')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.setSurfaceSize(100, 200);
                    },
                    child: Text('动态设置播放区域大小')),
                ElevatedButton(
                    onPressed: () {
                      print(widthD.floor().toInt());
                      print(heightD.floor().toInt());
                      ezvizUIKitControl.setSurfaceSize(
                          widthD.floor().toInt(), heightD.floor().toInt());
                    },
                    child: Text('恢复')),
                ElevatedButton(
                    onPressed: () {

                      ezvizUIKitControl.captureCamera("D21837011",1);
                    },
                    child: Text('截图')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandUp,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('上 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl1.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandUp,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('上2 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandUp,EZPTZAction.EZPTZActionSTOP,speed: 2);
                    },
                    child: Text('上 停止')),

                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandDown,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('下 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandDown,EZPTZAction.EZPTZActionSTOP,speed: 2);
                    },
                    child: Text('下 停止')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandLeft,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('左 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandLeft,EZPTZAction.EZPTZActionSTOP,speed: 2);
                    },
                    child: Text('左 停止')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandRight,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('右 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandRight,EZPTZAction.EZPTZActionSTOP,speed: 2);
                    },
                    child: Text('右 停止')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandZoomIn,EZPTZAction.EZPTZActionSTART,speed: 2);
                    },
                    child: Text('缩放in 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandZoomIn,EZPTZAction.EZPTZActionSTOP,speed: 2);
                    },
                    child: Text('缩放in 停止')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandZoomOut,EZPTZAction.EZPTZActionSTART,speed: 1);
                    },
                    child: Text('缩放out 开始')),
                ElevatedButton(
                    onPressed: () {
                      ezvizUIKitControl.controlPTZ("D21837011",1,EZPTZCommand.EZPTZCommandZoomOut,EZPTZAction.EZPTZActionSTOP,speed: 1);
                    },
                    child: Text('缩放out 停止')),
              ],
            )
          ],
        ));
  }
}

